/*
    日志消息类：描述一条日志信息需要包含的内容
    行号/时间/线程ID/日志器名称/文件名/日志消息/日志等级
*/
#ifndef __M_UTIL_H__
#define __M_UTIL_H__
#include <iostream>
#include <string>
#include <memory>
#include <thread>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
namespace zxlog
{
    class util
    {
    public:
        static time_t now() { return (size_t)time(nullptr); }
    };
    class file
    {
    public:
        static bool exists(const std::string &name)
        {
            struct stat t;
            //获取文件状态，成功返回0，错误返回-1，并设置 errno 来指示错误类型
            return stat(name.c_str(), &t) ==0;
        }
        static std::string path(const std::string &filename)
        {
            if(filename.empty())return".";
            auto pos = filename.find_last_of("/\\");
            if(pos == std::string::npos)
            return ".";
            return filename.substr(0,pos+1);
        }
        static void create_directory(const std::string &path)
        {
            if (path.empty())
                return;
            if (exists(path))
                return;
            size_t pos = 0, idx = 0;
            while (idx < path.size())
            {
                pos = path.find_first_of("/\\", idx);
                if (pos == std::string::npos)
                {
                    mkdir(path.c_str(), 0755);
                    return;
                }
                if (pos == idx)
                {
                    idx = pos + 1;
                    continue;
                }
                std::string sub = path.substr(0, pos);
                std::cout<<sub<<std::endl;
                if (sub == "." || sub == ".." || exists(sub))
                {
                    idx = pos + 1;
                    continue;
                }
                mkdir(sub.c_str(), 0755);
                idx = pos + 1;
            }
        }
    };
}
#endif